package cn.lyjuan.archetypes.bms.web.controller;


import cn.lyjuan.archetypes.bms.web.bean.vo.BaseResp;
import cn.lyjuan.archetypes.bms.web.bean.vo.PagerResp;
import cn.lyjuan.archetypes.bms.web.controller.base.SecurityController;
import cn.lyjuan.archetypes.bms.web.service.RoleService;
import cn.lyjuan.archetypes.bms.web.util.ErrUtils;
import cn.lyjuan.archetypes.bms.common.entity.MgManage;
import cn.lyjuan.archetypes.bms.common.entity.MgRole;
import cn.lyjuan.archetypes.bms.web.service.ActionService;
import cn.lyjuan.archetypes.bms.web.service.ManageService;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 角色控制器
 * Created by ly on 2015/3/6.
 */
@Controller
@RequestMapping("role/")
public class RoleController extends SecurityController
{
    @Autowired
    @Qualifier(value = "mgRoleService")
    private RoleService roleSer;

    @Autowired
    @Qualifier("mgManageService")
    private ManageService manageSer;

    @Autowired
    @Qualifier("mgActionService")
    private ActionService actionSer;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView toList()
    {
        return mv("/role/role_list");
    }

    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ResponseBody
    public PagerResp<MgRole> list(
            @RequestParam(defaultValue = "1")Integer pn,
            @RequestParam(required = false)Integer ps
    )
    {
        ps = this.getPs();// 消除空指针
        Map<String, String> param = this.paramsToMap();

        MgManage manage = manageSer.queryManageByName(this.getUserName());

        List<MgRole> list = roleSer.queryRolesForManage(manage, param, this.getPn(), this.getPs());

        return BaseResp.resp(list);
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public ModelAndView toAdd()
    {
        return mv("/role/role_edit");
    }

    /**
     * 添加角色
     *
     * @param role
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp addRole(MgRole role, HttpServletRequest req, HttpServletResponse resp)
    {
        MgManage manager = manageSer.queryManageByName(this.getUserName());

        roleSer.addRole(manager, role);

        return BaseResp.resp();
    }

    /**
     * 请求更新角色信息页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public ModelAndView toEdit(Integer id, Model model)
    {
        MgRole role = roleSer.queryRole(id);

        if (null == role)
            ErrUtils.appThrow("角色未找到或非您创建");

        this.pkgData(model, role);

        return mv("/role/role_edit");
    }

    /**
     * 更新角色信息
     *
     * @param name
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp edit(int id, String name) throws NotFoundException
    {
        MgRole fromRole = roleSer.queryRole(id);
        if (null == fromRole)
            ErrUtils.appThrow("角色未找到或非您创建");
        MgRole toRole = new MgRole();
        toRole.setName(name);
        toRole.setId(id);

        roleSer.saveRole(fromRole, toRole);

        return BaseResp.resp();
    }
}
